package com.bowlong.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import com.bowlong.lang.NumEx;
import com.bowlong.lang.StrEx;
import com.bowlong.objpool.StringBufPool;

public class SetForInt implements Set<Integer> {
	protected final Set<Integer> list = new HashSet<>();

	public SetForInt() {
	}

	// 1,2,3,4,5,6
	public SetForInt(String str) {
		if (StrEx.isEmpty(str))
			return;

		StringTokenizer st = new StringTokenizer(str);
		while (st.hasMoreTokens()) {
			String token = st.nextToken(",");
			int val = NumEx.stringToInt(token);
			list.add(val);
		}
	}

	public static final SetForInt create(String str) {
		if (str.startsWith("[")) // 检查是否可能是json
			return createJson(str);

		return new SetForInt(str);
	}

	public static final SetForInt createJson(String json) {
		if (json == null || json.length() <= 2)
			return new SetForInt();

		if (json.length() > 2)
			json = json.substring(1, json.length() - 1);
		return new SetForInt(json);
	}

	public static final SetForInt create(Collection<Integer> c) {
		SetForInt r2 = new SetForInt();
		r2.addAll(c);
		return r2;
	}

	@Override
	public int size() {
		return list.size();
	}

	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}

	@Override
	public boolean contains(Object o) {
		return list.contains(o);
	}

	@Override
	public Iterator<Integer> iterator() {
		return list.iterator();
	}

	@Override
	public Object[] toArray() {
		return list.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}

	@Override
	public boolean add(Integer e) {
		return list.add(e);
	}

	@Override
	public boolean remove(Object o) {
		return list.remove(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	@Override
	public boolean addAll(Collection<? extends Integer> c) {
		return list.addAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c);
	}

	@Override
	public void clear() {
		list.clear();
	}

	@Override
	public String toString() {
		StringBuffer sb = StringBufPool.borrowObject();
		try {
			return toString(sb).toString();
		} finally {
			StringBufPool.returnObject(sb);
		}
	}

	public StringBuffer toString(StringBuffer sb) {
		return toString(list, sb);
	}

	public static String toString(Collection<Integer> list) {
		StringBuffer sb = StringBufPool.borrowObject();
		try {
			return toString(list, sb).toString();
		} finally {
			StringBufPool.returnObject(sb);
		}
	}

	public static StringBuffer toString(Collection<Integer> list,
			StringBuffer sb) {
		for (Integer val : list) {
			sb.append(val).append(",");
		}
		if (sb.length() > 1)
			sb.deleteCharAt(sb.length() - 1);
		return sb;
	}

	public List<Integer> toList() {
		return toList(this);
	}

	public static List<Integer> toList(SetForInt s) {
		List<Integer> r2 = new ArrayList<>(s);
		return r2;
	}

}
